package com.example.module_home.device

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.viewModels
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_home.R
import com.example.module_home.adapter.DevicesListAdapter
import com.example.module_home.databinding.ActivityDeviceBinding
import com.example.module_home.vm.DeviceViewModel
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/DeviceActivity")
@AndroidEntryPoint
//设备管理
class DeviceActivity : BaseMvvmActivity<ActivityDeviceBinding>() {
    val deviceAdapter=DevicesListAdapter()

    private val deviceViewModel:DeviceViewModel by viewModels()

    override fun getLayoutId(): Int = R.layout.activity_device

    override fun initViews() {

        binding.deviceRv.adapter=deviceAdapter
        deviceViewModel.mSuccessLiveData.observe(this){
            deviceAdapter.data.clear()
            deviceAdapter.data.addAll(it.data.sortedByDescending { it.id})

            deviceAdapter.notifyDataSetChanged()
        }

        deviceViewModel.mErrorLiveData.observe(this){errorMsg->
            Toast.makeText(this,errorMsg,Toast.LENGTH_SHORT).show()
        }

        deviceAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/DeviceDetailActivity")
                .withSerializable("device",deviceAdapter.data[position])
                .navigation()

        }

        binding.deviceRv.addOnScrollListener(object :RecyclerView.OnScrollListener(){
            private var lastDy=0
            private var isDy=false
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                lastDy+=dy

                if (!isDy && lastDy >=800){
                    //onReach200Pixeles()
                    binding.add.visibility=View.VISIBLE
                    isDy=true
                }

                if (isDy && lastDy<800){
                    isDy =false

                    binding.add.visibility=View.GONE
                }
            }
        })
        //添加设备工单
        binding.add.setOnClickListener {
            ARouter.getInstance().build("/app/DeviceAddActivity").navigation()
        }

        //搜索
        binding.headerSearch.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                deviceViewModel.mSuccessLiveData.observe(this@DeviceActivity){
                    deviceAdapter.data.clear()
                    deviceAdapter.data.addAll(it.data.sortedByDescending { it.id }.filter { it.deviceName.contains(s.toString()) })
                    deviceAdapter.notifyDataSetChanged()
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })


    }

    override fun getActivityState(state: Bundle) {

    }

    override fun saveActivityState(outState: Bundle) {

    }

    override fun onResume() {
        super.onResume()
        deviceViewModel.getListData()
    }

}